/**
 * Created by ruiyuan on 15-11-23.
 */

var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?e68f7617995c06aa05dc86eaebf1a621";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();

var host = "http://app-66xiaoqu.babytree.com";

if (window.location.host.indexOf('t') == 0) {
    host = "http://app-66xiaoqu.babytree.com";
}

var params = {};
var tempArray = window.location.search.split('?');
if (tempArray.length == 2) {
    var parts = tempArray[1].split('&');
    for (var i = 0; i < parts.length; i++) {
        var nv = parts[i].split('=');
        if (!nv[0]) continue;
        params[nv[0]] = nv[1] || true;
    }
}

(function (w, d) {

    var array = w.location.href.split('?');

    var postData = {
        userId: params.token ? params.token : params.openId,
        type: 62,
        contentId:params.contentId,
        typeDesc: d.title
    };

    postData.ext = array.length > 1 ? (array[0] + '?' + encodeURIComponent(array[1])) : array[0];

    if (params.clickSource) {
        postData.clickSource = Number(params.clickSource)
    }

    postData = (function (obj) { // 转成post需要的字符串.
        var str = "";
        for (var prop in obj) {
            str += prop + "=" + obj[prop] + "&"
        }
        return str;
    })(postData);

    var xhr = new XMLHttpRequest();

    xhr.open("POST", host + "/rest/v1.0/appLog/clickAcquisition/" + params.deviceId + '?vcode=' + params.vcode + '&type=' + params.type, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function () {
        var XMLHttpReq = xhr;
        if (XMLHttpReq.readyState == 4) {
            if (XMLHttpReq.status == 200) {
                var text = XMLHttpReq.responseText;

                console.log(text);
            }
        }
    };
    xhr.send(postData);

}(window, document));


function clickAcquisition(postData) {

    postData.userId = params.token ? params.token : params.openId;
    if (postData.ext == null) {
        var array = window.location.href.split('?');
        postData.ext = array.length > 1 ? (array[0] + '?' + encodeURIComponent(array[1])) : array[0];
    }


    postData = (function (obj) { // 转成post需要的字符串.
        var str = "";
        for (var prop in obj) {
            str += prop + "=" + obj[prop] + "&"
        }
        return str;
    })(postData);

    var xhr = new XMLHttpRequest();

    xhr.open("POST", host + "/rest/v1.0/appLog/clickAcquisition/" + params.deviceId + '?vcode=' + params.vcode + '&type=' + params.type, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function () {
        var XMLHttpReq = xhr;
        if (XMLHttpReq.readyState == 4) {
            if (XMLHttpReq.status == 200) {
                var text = XMLHttpReq.responseText;

                console.log(text);
            }
        }
    };
    xhr.send(postData);
}